<?php
/**
 * Created by PhpStorm.
 * User: John
 * Date: 17-2-6
 * Time: 下午6:04
 */
namespace Business;

use think\Controller;
use app\backend\model\Member;
use Cache\Redis;
use Business\sms\YunTongXunSMS;

class Business extends Controller
{

    /**
     * 生成会员维一登录账户
     *
     * @param $dtype 账户类型  0 超管，1 业主， 2租户, 3托管公司, 4运营方, 5物业公司, 6水电公司, 7员工
     * @return string
     */
    public function _generateAccount($dtype)
    {
        $username_need = array(0=>1000, 1=>1000000,2=>4000000,3=>2000000,4=>3000000,5=>9000000,6=>10000000, 7=> 11000000);
        if (!array_key_exists($dtype, $username_need)) {
            return 1;
        }

        $where = '';
        if ($dtype==0) {
            //超管
            $where = "cast(`username` as UNSIGNED) between 1000 and 9999";
        } elseif ($dtype==1) {
            //业主
            $where = "cast(`username` as UNSIGNED) between 1000000 and 1999999";
        } elseif ($dtype==2) {
            //租户
            $where = "cast(`username` as UNSIGNED) between 4000000 and 8999999";
        } elseif ($dtype==3) {
            //托管公司
            $where = "cast(`username` as UNSIGNED) between 2000000 and 2999999";
        } elseif ($dtype==4) {
            //运营方
            $where = "cast(`username` as UNSIGNED) between 3000000 and 3999999";
        } elseif ($dtype==5) {
            //物业
            $where = "cast(`username` as UNSIGNED) between 9000000 and 9999999";
        } elseif ($dtype==6) {
            //水电
            $where = "cast(`username` as UNSIGNED) between 10000000 and 10999999";
        } elseif ($dtype == 7) {
            //员工
            $where = "cast(`username` as UNSIGNED) between 11000000 and 11999999";
        }
        $field = 'MAX(username)+1 as passport';
        $memberModel = new Member();
        $res = $memberModel->getMemberInfo($where, $field);
        $ret = !is_null($res[0]['passport']) ? $res[0]['passport'] : $username_need[$dtype];
        return $ret;
    }

    /**
     * 发送验证码
     * @date   2017-02-09
     * @author Lanlc
     *
     * @param  string  $mobile   手机号
     * @param  string  $key      redis值前缀
     *
     * @return
     */
    public function sendCode($mobile, $key)
    {
        $redis = new Redis();
        $codeTime = $redis->get("$key:$mobile");
        if ($codeTime) {
            list($code, $time) = explode('|', $codeTime);
            if ($_SERVER['REQUEST_TIME']-$time < 120) {
                return ['code'=>141];
            }
        }

        $code = genRandomNum(6);
        $cache = $code . '|' . $_SERVER['REQUEST_TIME'];
        $redis->set("$key:$mobile", $cache, 600); //10分钟内有效

        $yunTongXun = new YunTongXunSMS();
        $res = $yunTongXun->doSendSMS($mobile, [0 => $code, 1=>'10分钟'], 171333);
        return $res;
    }

    /**
     * 发送催缴短信
     * @date   2017-04-25
     * @author wuhao
     *
     * @param  string  $mobile   手机号
     * @param  array   $datas    数据内容
     * @param  array   $content  要发送的内容
     * @param  int     $contractType 合同类型 1=租赁 2=托管
     *
     * @return array
     */
    public function sendPayNotice($mobile, $datas, $content, $contractType = 1)
    {
        if ($contractType == 1) {
            $tplId = 182857;//模板id
        } else {
            $tplId = 1;
        }
        $yunTongXun = new YunTongXunSMS();
        $res = $yunTongXun->sendPayNotice($mobile, $datas, $content, $tplId);
        return $res;
    }

    /**
     * 发送到账短信
     * @date   2017-04-26
     * @author wuhao
     *
     * @param  string  $mobile   手机号
     * @param  array   $datas    数据内容
     * @param  array   $content  要发送的内容
     * @param  int     $contractType 合同类型 1=租赁 2=托管
     * @param  int     $sendMember 发送对象 1=付款方 2=收款方
     *
     * @return array
     */
    public function sendIncomeNotice($mobile, $datas, $content, $contractType = 1, $sendMember = 1)
    {
        if ($contractType == 1) {
            if ($sendMember == 1) {
                $tplId = 194785;//模板id
            } else {
                $tplId = 194782;//模板id
            }
        } else {
            $tplId = 1;//模板id
        }

        $yunTongXun = new YunTongXunSMS();
        $res = $yunTongXun->sendIncomeNotice($mobile, $datas, $content, $sendMember, $tplId);
        return $res;
    }
}
